package steveshrader.budget.client.widgets;

import steveshrader.budget.client.events.ExpensesChangeEvent;
import steveshrader.budget.shared.BudgetHelper;

import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TextHeader;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Widget;

/**
 * A table with summaries of all expenses.
 */
public class ExpenseSummaryWidget extends Composite {

  interface Binder extends UiBinder<Widget, ExpenseSummaryWidget> {
  }

  interface Style extends CssResource {
  }

  interface TableResources extends CellTable.Resources {
    @Source(value = {CellTable.Style.DEFAULT_CSS, "CellTableStyle.css"})
    CellTable.Style cellTableStyle();
  }

  private class ExpenseTypeColumn extends Column<ExpensesChangeEvent.ExpenseSummaryEntry, String> {
    public ExpenseTypeColumn() {
      super(new TextCell());
    }

    @Override
    public String getValue(ExpensesChangeEvent.ExpenseSummaryEntry object) {
      return object.getCategory();
    }
  }

  private class AmountColumn extends Column<ExpensesChangeEvent.ExpenseSummaryEntry, String> {
    public AmountColumn() {
      super(new TextCell());
    }

    @Override
    public String getValue(ExpensesChangeEvent.ExpenseSummaryEntry object) {
      return BudgetHelper.convertAmount(object.getAmount());
    }
  }


  @UiField
  DockLayoutPanel dock;

  @UiField(provided = true)
  CellTable<ExpensesChangeEvent.ExpenseSummaryEntry> table;

  public ExpenseSummaryWidget(EventBus eventBus) {
    table = new CellTable<ExpensesChangeEvent.ExpenseSummaryEntry>(15, GWT.<TableResources> create(TableResources.class));
    initWidget(GWT.<Binder> create(Binder.class).createAndBindUi(this));
    dock.getWidgetContainerElement(table).getStyle().setProperty("overflowY", "visible");

    TextHeader header = new TextHeader("Expense Summary");
    
    Column<ExpensesChangeEvent.ExpenseSummaryEntry, String> expenseTypeColumn = new ExpenseTypeColumn();
    table.addColumn(expenseTypeColumn, header);
    table.setColumnWidth(expenseTypeColumn, "6ex");
    Column<ExpensesChangeEvent.ExpenseSummaryEntry, String> amountColumn = new AmountColumn();
    amountColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LOCALE_END);
    table.addColumn(amountColumn, header);
    table.setColumnWidth(amountColumn, "6ex");
    
    table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.DISABLED);

    ExpensesChangeEvent.register(eventBus, new ExpensesChangeEvent.Handler() {
      public void onExpensesChanged(ExpensesChangeEvent e) {
    	table.setRowData(e.getSummaries());
      }
    });
  }
}
